package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.util.UUID;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;

import com.alee.extended.image.WebImage;
import com.alee.laf.text.WebTextField;
import com.it2297.model.UsedItem.ItemCondition;
import com.it2297.model.UsedItem.Status;
import com.it2297.util.WrapLayout;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.Dimension;

@SuppressWarnings("serial")
public class UsedItemCreateUpdateDialog extends JDialog
{
	private int id;
	private String uuid;
	private final CardLayout scenes = new CardLayout(0, 0);
	private DialogType dialogType;

	private final JPanel panel = new JPanel();
	private final JPanel panel_1 = new JPanel();
	private final JTextPane txtpnToRegisterAn = new JTextPane();
	private final JLabel lblTitle = new JLabel("Title:");
	private final JLabel lblDescription = new JLabel("Description:");
	private final JLabel lblType = new JLabel("Type:");
	private final JLabel lblQuantity = new JLabel("Quantity:");
	private final JLabel lblItemCondition = new JLabel("Item Condition:");
	private final JLabel lblComment = new JLabel("Comment:");
	private final JLabel lblDonorNric = new JLabel("Donor NRIC:");
	private final JPanel scene = new JPanel();
	private final JPanel panel_2 = new JPanel();
	private final JButton btnNext = new JButton("Next");
	private final JButton btnPrevious = new JButton("Previous");
	private final JButton btnCancel = new JButton("Cancel");
	private final JTextField txtTitle = new JTextField();
	private final JComboBox cbType = new JComboBox();
	private final JSpinner spQuantity = new JSpinner();
	private final JComboBox cbItemCondition = new JComboBox();
	private final WebTextField txtDonorNRIC = new WebTextField();
	private final JScrollPane scrollPane = new JScrollPane();
	private final JTextArea taDescription = new JTextArea();
	private final JScrollPane scrollPane_1 = new JScrollPane();
	private final JTextArea taComment = new JTextArea();
	private final JTextPane textPane = new JTextPane();
	private final JButton btnUploadMultipleImages = new JButton("Browse...");
	private final JScrollPane scrollPane_2 = new JScrollPane();
	private final JPanel panel_3 = new JPanel();
	private final JPanel panel_4 = new JPanel();
	private final JButton btnFinish = new JButton("Finish");
	private final JLabel lblApplicationStatus = new JLabel("Application Status:");
	private final JComboBox cbApplicationStatus = new JComboBox();
	private final JTextPane txtpnLogMessage = new JTextPane();

	/**
	 * Create the dialog.
	 */
	public UsedItemCreateUpdateDialog()
	{
		initGUI();
	}

	private void initGUI()
	{
		this.setSize(600, 450);
		this.setLocationRelativeTo(null);
		this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		getContentPane().setLayout(new BorderLayout(0, 0));

		getContentPane().add(scene, BorderLayout.CENTER);
		scene.setLayout(scenes);
		panel.setName("1");

		scene.add(panel);
		panel.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("max(70dlu;default)"), FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("left:default"), FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"), FormFactory.RELATED_GAP_COLSPEC, }, new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode("default:grow"), FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode("default:grow"), FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, }));
		txtpnToRegisterAn.setEditable(false);
		txtpnToRegisterAn.setContentType("text/html");
		txtpnToRegisterAn.setText("<html><body style ='font-family:" + txtpnToRegisterAn.getFont() + "'>" + "<h3>Step 1: </h3>" + "<p>Information:</p>" + "</body>" + "</html>");
		txtpnToRegisterAn.setOpaque(false);

		panel.add(txtpnToRegisterAn, "2, 2, 5, 1, fill, fill");

		panel.add(lblTitle, "2, 4");

		txtTitle.setColumns(10);
		panel.add(txtTitle, "4, 4, 3, 1, fill, default");

		panel.add(lblDescription, "2, 6, default, top");
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		panel.add(scrollPane, "4, 6, 3, 1, fill, fill");
		taDescription.setLineWrap(true);

		scrollPane.setViewportView(taDescription);

		panel.add(lblType, "2, 8");

		panel.add(cbType, "4, 8, 3, 1, left, default");

		panel.add(lblQuantity, "2, 10");
		spQuantity.setPreferredSize(new Dimension(100, 28));

		panel.add(spQuantity, "4, 10, 3, 1, left, default");

		panel.add(lblItemCondition, "2, 12");

		panel.add(cbItemCondition, "4, 12, 3, 1, left, default");

		panel.add(lblComment, "2, 14, default, top");
		scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		panel.add(scrollPane_1, "4, 14, 3, 1, fill, fill");
		taComment.setLineWrap(true);

		scrollPane_1.setViewportView(taComment);

		panel.add(lblDonorNric, "2, 16");

		txtDonorNRIC.setTrailingComponent(new WebImage(new ImageIcon(UsedItemCreateUpdateDialog.class.getResource("/com/alee/extended/optionpane/icons/warning.png"))));
		txtDonorNRIC.setColumns(10);
		panel.add(txtDonorNRIC, "4, 16, left, default");

		panel.add(lblApplicationStatus, "2, 18, right, default");

		panel.add(cbApplicationStatus, "4, 18, 4, 1, left, default");
		panel_1.setName("2");
		scene.add(panel_1);
		panel_1.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC, FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("default:grow"), FormFactory.RELATED_GAP_COLSPEC, }, new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode("default:grow"), }));
		textPane.setContentType("text/html");
		textPane.setOpaque(false);
		textPane.setText("<html><body style ='font-family:" + txtpnToRegisterAn.getFont() + "'>" + "<h3>Step 2: </h3>" + "<p>Upload image for the item.<br/>Ctrl-Select or Shift-Select to upload multiple images.<br/>Double-click on the image to remove from the list:</p>" + "</body>" + "</html>");

		panel_1.add(textPane, "2, 2, 3, 1, fill, fill");

		panel_1.add(btnUploadMultipleImages, "2, 4");

		scrollPane_2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		panel_1.add(scrollPane_2, "2, 6, 3, 1, fill, fill");

		panel_3.setLayout(new WrapLayout(WrapLayout.LEFT, 5, 5));
		scrollPane_2.setViewportView(panel_3);

		FlowLayout flowLayout = (FlowLayout) panel_2.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);

		getContentPane().add(panel_2, BorderLayout.SOUTH);

		panel_2.add(btnCancel);
		btnPrevious.setEnabled(false);

		panel_2.add(btnPrevious);

		panel_2.add(btnNext);
		btnFinish.setVisible(false);
		btnFinish.setEnabled(false);

		panel_2.add(btnFinish);

		panel_4.setName("3");

		scene.add(panel_4, "name_7284198443269");
		panel_4.setLayout(new FormLayout(new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC, ColumnSpec.decode("598px:grow"), }, new RowSpec[] { FormFactory.RELATED_GAP_ROWSPEC, RowSpec.decode("15px:grow"), }));
		txtpnLogMessage.setContentType("text/html");
		txtpnLogMessage.setText("<html><body style ='font-family:" + txtpnLogMessage.getFont() + "'><h3>Successful!</h3><p>Item registered successfully.</p></body></html>");
		txtpnLogMessage.setOpaque(false);
		txtpnLogMessage.setEditable(false);

		panel_4.add(txtpnLogMessage, "2, 2, fill, fill");
	}

	public void reset()
	{
		scenes.first(scene);
		txtTitle.setText(null);
		taDescription.setText(null);
		taComment.setText(null);
		txtDonorNRIC.setText(null);
		panel_3.removeAll();
		panel_3.revalidate();
		panel_3.repaint();
	}

	public void setComboBoxTypes(Object[] types)
	{
		cbType.setModel(new DefaultComboBoxModel(types));
	}

	public void setComboBoxItemConditions(Object[] itemConditions)
	{
		cbItemCondition.setModel(new DefaultComboBoxModel(itemConditions));
	}

	public void setComboBoxApplicationStatus(Object[] applicationStatus)
	{
		cbApplicationStatus.setModel(new DefaultComboBoxModel(applicationStatus));
	}

	public JPanel getPreviewPanel()
	{
		return panel_3;
	}

	public JPanel getScenesPanel()
	{
		return scene;
	}

	public JButton getCancelButton()
	{
		return btnCancel;
	}

	public JButton getPreviousButton()
	{
		return btnPrevious;
	}

	public JButton getNextButton()
	{
		return btnNext;
	}

	public JButton getFinishButton()
	{
		return btnFinish;
	}

	public String getTitle()
	{
		return txtTitle.getText();
	}

	public void setTitle(String title)
	{
		txtTitle.setText(title);
	}

	public String getDescription()
	{
		return taDescription.getText();
	}

	public void setDescription(String description)
	{
		taDescription.setText(description);
	}

	public String getItemType()
	{
		return cbType.getSelectedItem().toString();
	}

	public void setItemType(String itemType)
	{
		cbType.setSelectedItem(itemType);
	}

	public int getQuantity()
	{
		return (Integer) (spQuantity.getValue());
	}

	public void setQuantity(int quantity)
	{
		spQuantity.setValue(quantity);
	}

	public ItemCondition getItemCondition()
	{
		return (ItemCondition) (cbItemCondition.getSelectedItem());
	}

	public void setItemCondition(ItemCondition itemCondition)
	{
		cbItemCondition.setSelectedItem(itemCondition);
	}

	public Status getApplicationStatus()
	{
		return (Status) (cbApplicationStatus.getSelectedItem());
	}

	public void setApplicationStatus(Status applicationStatus)
	{
		cbApplicationStatus.setSelectedItem(applicationStatus);
	}

	public String getComment()
	{
		return taComment.getText();
	}

	public void setComment(String comment)
	{
		taComment.setText(comment);
	}

	public String getDonorNRIC()
	{
		return txtDonorNRIC.getText();
	}

	public void setDonorNRIC(String donorNRIC)
	{
		txtDonorNRIC.setText(donorNRIC);
	}

	public WebTextField getDonorNRICField()
	{
		return txtDonorNRIC;
	}

	public String getUuid()
	{
		return uuid;
	}

	public void setUuid(String uuid)
	{
		this.uuid = uuid;
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public enum DialogType
	{
		CREATE, UPDATE;
	}

	public void addDonorNRICFocusListener(FocusAdapter focusAdapter)
	{
		txtDonorNRIC.addFocusListener(focusAdapter);
	}

	public void clearDonorNRIC()
	{
		txtDonorNRIC.clear();
	}

	public void addUploadActionListener(ActionListener actionListener)
	{
		btnUploadMultipleImages.addActionListener(actionListener);
	}

	public void addCancelActionListener(ActionListener actionListener)
	{
		btnCancel.addActionListener(actionListener);
	}

	public void previous()
	{
		scenes.previous(scene);
	}

	public void addPreviousActionListener(ActionListener actionListener)
	{
		btnPrevious.addActionListener(actionListener);
	}

	public void addNexActionListener(ActionListener actionListener)
	{
		btnNext.addActionListener(actionListener);
	}

	public void addFinishActionListener(ActionListener actionListener)
	{
		btnFinish.addActionListener(actionListener);
	}

	public void next()
	{
		scenes.next(scene);
	}

	public DialogType getDialogType()
	{
		return dialogType;
	}

	public void setDialogType(DialogType dialogType)
	{
		this.dialogType = dialogType;
	}

}
